package com.example.teaknower.fragment;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.teaknower.R;
import com.example.teaknower.activity.MainActivity;
import com.example.teaknower.activity.ZhiDaoDetailsActivity;
import com.example.teaknower.adapter.RecycleViewAdapter;
import com.example.teaknower.model.Tea;
import com.example.teaknower.utils.GlideImageLoader;
import com.example.teaknower.utils.OkHttpUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TabFragment extends Fragment {
    @BindView(R.id.recycler_tab)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    private List<Tea> mdata = new ArrayList<>();
    private List<String> imageUrl = new ArrayList<>();
    int mPosition;
    private RecycleViewAdapter mAdapter;
    private MainActivity MainActivity;
    private Banner mBanner;
    private String tabName;

    public TabFragment(String name) {
        this.tabName = name;
        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Tea/findAllBySort?sort=" + this.tabName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(s);
        System.out.println("name = " + this.tabName);
        System.out.println("data = " + jsonObject.get("data"));
        Map<String, Object> map = (Map<String, Object>)jsonObject.get("data");
        List<Object> da = (List<Object>)map.get("content");
        System.out.println("da = " + da);
        System.out.println("da.size = " + da.size());
        for(int i = 0 ; i < da.size() ; i++ ){
            Map<String,Object> m = (Map<String,Object>) da.get(i);
            String id = m.get("id").toString();
            String url = m.get("url").toString();
            String wine_brand = m.get("tea_brand").toString();
            String wine_price = m.get("tea_price").toString();
            String wine_sort = m.get("tea_sort").toString();
            String wine_img = m.get("tea_img").toString();
            String wine_title = m.get("tea_title").toString();
            Tea tea = new Tea(id, url, wine_brand, wine_price, wine_sort, wine_img, wine_title);
            mdata.add(tea);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmeny_tab, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        mPosition = getArguments().getInt("position");
        initView();
        initData();

        return rootView;
    }


    private void initView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new RecycleViewAdapter(R.layout.home_item_view, mdata);

        View top = getLayoutInflater().inflate(R.layout.layout_banner, (ViewGroup) mRecyclerView.getParent(), false);
        mBanner = top.findViewById(R.id.banner);
        mAdapter.addHeaderView(top);
        mRecyclerView.setAdapter(mAdapter);
        MainActivity = com.example.teaknower.activity.MainActivity.getMainActivity();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                //Toast.makeText(MainActivity,"点击文章的id为 ： " + mdata.get(position).getId(),Toast.LENGTH_LONG).show();
                Log.e("知道中点击文章的id",""+mdata.get(position).getId());
                Intent intent = new Intent(MainActivity, ZhiDaoDetailsActivity.class);
                intent.putExtra("url",mdata.get(position).getUrl());
                MainActivity.startActivity(intent);
            }
        });

    }

    private void initData() {
//        for (int i = 0; i < 20; i++) {
//            mdata.add("TabFragment_" + mPosition + "第" + i + "条数据");
//        }
        mAdapter.setNewData(mdata);//模拟网络请求成功后要调用这个方法刷新数据
        if (mPosition == 0) {
            imageUrl.clear();
            imageUrl.add("http://mpic.tiankong.com/aa4/fd8/aa4fd84a633298f43fe4521ba9a2dcbc/640.jpg");
            imageUrl.add("http://mpic.tiankong.com/34d/ee2/34dee24f36c176651e0b64dbc8f5d170/640.jpg");
            imageUrl.add("http://mpic.tiankong.com/5a4/a2d/5a4a2dc36ad6d42ba95ee8c2afd8e038/640.jpg");
            initBanner(imageUrl);
        } else {
            mBanner.setVisibility(View.GONE);
        }

    }

    private void initBanner(List<String> imageUrl) {
        mBanner.setImages(imageUrl)
                .setImageLoader(new GlideImageLoader())
                .setDelayTime(3000)
                .start();
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        mBanner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        mBanner.stopAutoPlay();
    }
}
